package com.oracle.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oracle.compents.ApiResponse;
import com.oracle.compents.CodeCompents;
import com.oracle.pojo.Equipment;
import com.oracle.pojo.Publicity;
import com.oracle.service.PublicityService;
import com.oracle.vo.DeleteRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/publicity")
public class PublicityController {

    @Autowired
    private PublicityService publicityService;

    @PostMapping("/search")
    public ApiResponse search(@RequestBody Publicity publicity,Integer pageNum , Integer pageSize){
        IPage<Publicity> publicity1 = publicityService.findPublicity(publicity, pageNum, pageSize);
        if (ObjectUtils.isEmpty(publicity1)){
            ApiResponse<Object> failed = ApiResponse.failed(null);
            failed.setMsg("公告栏模块，查询不到公告");
            return failed;
        }else{
            return ApiResponse.success(publicity1);
        }
    }

    @PostMapping("/insert")
    public ApiResponse insert(@RequestBody Publicity publicity){
        publicity.setTipState(CodeCompents.PUBLICITY_STATE);
        int i = publicityService.insertPublicity(publicity);
        if (i>0){
            return ApiResponse.success(null);
        }else {
            return ApiResponse.failed(null);
        }
    }

    @GetMapping("/findOne")
    public ApiResponse findOne(Integer id){
        Publicity onePublicity = publicityService.findOnePublicity(id);
        if (ObjectUtils.isEmpty(onePublicity)){
            return ApiResponse.failed(null);
        }else {
            return ApiResponse.success(onePublicity);
        }
    }

    @GetMapping("/findOneByState")
    public ApiResponse findOneByState(Integer state){
        Publicity onePublicity = publicityService.findOnePublicityByState(state);
        if (ObjectUtils.isEmpty(onePublicity)){
            return ApiResponse.failed(null);
        }else {
            return ApiResponse.success(onePublicity);
        }
    }

    @PostMapping("/updataPublicity")
    public ApiResponse updataPublicity(@RequestBody Publicity publicity){
        int i = publicityService.updatePublicity(publicity);
        if (i>0){
            return ApiResponse.success(null);
        }else {
            return ApiResponse.failed(null);
        }
    }

    @PostMapping("/delete")
    public ApiResponse delete(@RequestBody DeleteRequest deleteRequest){
        try {
            List<Integer> ids = deleteRequest.getIds();
            int i = publicityService.deletePublicity(ids);
            if (i > 0){
                return ApiResponse.success(null);
            }else {
                return ApiResponse.failed(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failed(null);
        }
    }

}
